const express = require('express');
const router = express.Router();
const db =  require("../../Models");
const md5 = require('md5');
const config = require("../../config/config.json")['rests'];


router.get("/", async (req,res,next)=>{
    res.render("login/login",{
        title: '登录'
    })
})


//登录
router.post("/doLogin",async (req,res,next)=>{
    let md5Password = md5(req.body.password + config.secretKey)
    
    let result = await db.user.findOne({
        where:{name: req.body.name},
        attributes: ['password']
    })

    if(result != null && result.password === md5Password){
        req.session.username = req.body.name;
        res.json(true);
    }else{
        res.json(false);
    }
 })


// 退出登录
router.get('/outLogin',(req,res,next)=>{
    req.session.username = null;
    res.json(true)
})


module.exports = router;